package com.cooqe.collection_snmp.snmpfinder;

import com.alibaba.fastjson.JSONObject;
import com.cooqe.basefinder.AbstractBaseFinder;
import com.cooqe.collection_snmp.model.SnmpParameter;
import com.cooqe.collection_snmp.utils.SnmpUtil;
import com.cooqe.entity.ItMetric;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2023/11/14-2023
 * @Description: snmp 进行采集
 */
@Component(value = "snmpFinderJob")
@Slf4j
public class SnmpFinderJob extends AbstractBaseFinder {

    @Autowired
    private SnmpGetHandle snmpGetHandle;

    @Override
    //@XxlJob("snmpFinderJob")
    public void schedulingExecute(String params) {
        super.schedulingExecute(params);
    }

    @Override
    public void beforeCollection(JSONObject discoverParam) {
        log.info("beforeCollection 采集前置方法执行 ......");
        //if (this.threadLocal.get()==null) {return;}
        //模版参数
        String ip = discoverParam.getString("ip");
        Integer port = discoverParam.getInteger("port");
        Integer version = discoverParam.getInteger("version");
        String community = discoverParam.getString("community");
        String securityName = discoverParam.getString("securityName");
        Integer securityLevel = discoverParam.getInteger("securityLevel");
        String authProtocol = discoverParam.getString("authProtocol");
        String authPassphrase = discoverParam.getString("authPassphrase");
        String privacyProtocol = discoverParam.getString("privacyProtocol");
        String privacyPassphrase = discoverParam.getString("privacyPassphrase");

        SnmpParameter snmpParameter = new SnmpParameter();
        snmpParameter.setIp(ip);
        snmpParameter.setPort(port);
        snmpParameter.setVersion(version);
        snmpParameter.setCommunity(community);
        snmpParameter.setSecurityName(securityName);
        snmpParameter.setSecurityLevel(securityLevel);
        snmpParameter.setAuthProtocol(authProtocol);
        snmpParameter.setAuthPassphrase(authPassphrase);
        snmpParameter.setPrivacyProtocol(privacyProtocol);
        snmpParameter.setPrivacyPassphrase(privacyPassphrase);

        //创建 snmp、target
        Snmp snmp = SnmpUtil.createSnmp();
        Target target = SnmpUtil.createTarget(snmpParameter, snmp);

        //封装 threadLocal
        Map<String, Object> map = new HashMap<>();
        map.put("snmpParameter", snmpParameter);
        map.put("target", target);
        map.put("snmp", snmp);
        this.threadLocal.set(map);
    }

    @Override
    public void doCollection(List<ItMetric> metricList) {
        //参数
        Map<String, Object> paramMap = this.threadLocal.get();
        SnmpParameter snmpParameter = (SnmpParameter) paramMap.get("snmpParameter");
        Target target = (Target) paramMap.get("target");
        Snmp snmp = (Snmp) paramMap.get("snmp");

        //依次采集每一个指标
        for (ItMetric metric : metricList) {
            String collectParams = metric.getCollectParams();
            if (StringUtils.isNotBlank(collectParams)) {
                JSONObject collectParamsObj = JSONObject.parseObject(collectParams);
                String method = collectParamsObj.getString("method");
                try {
                    switch (method) {
                        case "get":
                            log.info("snmp get ...");
                            snmpParameter.setMethodType(PDU.GET);
                            Map<String, List<String>> getResultMap = snmpGetHandle.snmpGet(collectParamsObj, snmpParameter, target, snmp);
                            break;
                        case "getNext":
                            log.info("snmp getNext ...");
                            snmpParameter.setMethodType(PDU.GETNEXT);
                            Map<String, List<String>> getNextResultMap = snmpGetHandle.snmpGet(collectParamsObj, snmpParameter, target, snmp);
                            break;
                        case "set":
                            log.info("snmp set ...");
                            Map<String, List<String>> setResultMap = SnmpSetHandle.snmpSet(collectParamsObj, snmpParameter, target, snmp);
                            break;
                        case "getBulk":
                            log.info("snmp getBulk ...");
                            Map<String, List<String>> getBulkResultMap = SnmpBulkHandle.snmpBulk(collectParamsObj, snmpParameter, target, snmp);
                            break;
                        case "walk":
                            log.info("snmp walk ...");
                            Map<String, List<String>> walkResultMap = SnmpWalkHandle.snmpWalk(collectParamsObj, snmpParameter, target, snmp);
                            break;
                        // ......
                        default:
                            log.info("default ...");
                    }
                }catch (Exception exception){
                    log.error(exception.getMessage());
                }
            }
        }
    }
}